package com.example.adapter_sdk.quick;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<BaseViewHolder>{
    private List<T> data = new ArrayList<>();
    private int layoutId;
    private int index;
    private int sum;
    private static final int HEADTYPE = -1;
    private static final int FOOTTYPE = -2;
    private static final int CONTENTTYPE = 0;
    private int count = 0;
    private HeadViewHolder headViewHolder;
    private FootViewHolder footViewHolder;
    private OnItemClickListener onItemClickListener;
    private OnItemChildClickListener onItemChildClickListener;
    private List<Integer>ids = new ArrayList<>();
    public BaseQuickAdapter(int layoutId) {
        this.layoutId = layoutId;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==HEADTYPE){
            return headViewHolder;
        }
        if (viewType==FOOTTYPE){
            return footViewHolder;
        }
        View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, null, false);
        BaseViewHolder baseViewHolder = new BaseViewHolder(itemView);
        bindListener(baseViewHolder);
        return baseViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder||holder instanceof FootViewHolder){
            return;
        }
        T item=data.get(position-(headViewHolder==null?0:1));
        convert(holder,item);
    }

    public abstract void convert(BaseViewHolder holder,T item);
    @Override
    public int getItemCount() {
        return data.size()+count;
    }
    //获取适配器关联数据源
    public List<T> getData(){
        return data;
    }
    public void add(T entity){
        data.add(entity);
        notifyItemInserted(data.size()-1);
    }
//    添加多条数据
    public void adds(List<T>list){
        if (list==null) return;
        if (list.size()==0)return;
        int position = data.size() - 1;
        data.addAll(list);
        notifyItemRangeChanged(position,list.size());
    }
    public void remove(int position){
        if (position<0)return;
        if (position>=data.size())return;
        data.remove(position);
        notifyItemRemoved(position);
    }
//    设置新数据
    public void setNewInstance(List<T>list){
        if (data.size() == 0){
            data.addAll(list);
            notifyDataSetChanged();
            return;
        }
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new BaseQuickDiff<T>(data, list));
        diffResult.dispatchUpdatesTo(new ListUpdateCallback() {
            @Override
            public void onInserted(int position, int count) {
                List<T> subList = list.subList(position, count);
                data.addAll(position,subList);
                index=position;
                sum=count;
            }

            @Override
            public void onRemoved(int position, int count) {
                int i = position+count-1;
                for (; i >=position; i--) {
                    data.remove(i);
                }
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {

            }

            @Override
            public void onChanged(int position, int count, @Nullable Object payload) {
                onRemoved(position,count);
                onInserted(position,count);
            }
        });
        notifyItemRangeChanged(index,sum+(headViewHolder==null?0:1));
    }
//    ========================添加头尾布局======================

    public void setHeaderView(View headerView){
        if (headViewHolder==null){
            LinearLayout linearLayout = new LinearLayout(headerView.getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(layoutParams);
            headViewHolder=new HeadViewHolder(linearLayout);
            count+=1;
        }
        headViewHolder.rootView.addView(headerView);
    }
    public void setFootView(View footView){
        if (footViewHolder==null){
            LinearLayout linearLayout = new LinearLayout(footView.getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(layoutParams);
            footViewHolder=new FootViewHolder(linearLayout);
            count+=1;
        }
        footViewHolder.rootView.addView(footView);
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0&&headViewHolder!=null){
            return HEADTYPE;
        }else if (position==(getItemCount()-1)&&footViewHolder!=null){
            return FOOTTYPE;
        }
        return CONTENTTYPE;
    }

    //    关联到recyclerview获得布局管理器判断是哪一种布局
    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager){
            GridLayoutManager gridLayoutManager= (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
//                    合并列
                    if (position==0&&headViewHolder!=null){
                        return gridLayoutManager.getSpanCount();
                    }
                    if (position==getItemCount()-1&&footViewHolder!=null){
                        return gridLayoutManager.getSpanCount();
                    }
                    return 1;
                }
            });
        }
    }
//    ========================点击事件======================
//      添加子控件监听方法
        protected void addItemChildClick(int ... id){
            for ( int i = 0;i < id.length;i ++){
                ids.add(id[i]);
            }
        }
        public void setOnItemClickListener(OnItemClickListener onItemClickListener){
            this.onItemClickListener=onItemClickListener;
        }
        public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener){
            this.onItemChildClickListener=onItemChildClickListener;
        }
    private void bindListener(BaseViewHolder holder){
        //绑定item点击事件
        if (onItemClickListener!=null)
            holder.itemView.setOnClickListener(v->{
                onItemClickListener.onItemClick(this,holder.getAdapterPosition()-(headViewHolder==null?0:1));
            });
        if (onItemChildClickListener != null){
            for (Integer id : ids){
                holder.getView(id).setOnClickListener(v->{
                    onItemChildClickListener.onItemChildClick(this,id,holder.getAdapterPosition()-(headViewHolder==null?0:1));
                });
            }
        }
    }
        public interface OnItemClickListener{
            void onItemClick(RecyclerView.Adapter adapter,int position);
        }
        public interface OnItemChildClickListener{
            void onItemChildClick(RecyclerView.Adapter adapter,int id,int position);
        }
}
